package com.study.dataStructure.leetcode.problems;

import com.sun.org.apache.xalan.internal.res.XSLTErrorResources;

/**
 * @author tiny
 * @date 2022/5/10 23:14
 * @Description: 13.罗马数字转整数
 */
public class RomanToInt {

    public static void main(String[] args) {
        RomanToInt demo = new RomanToInt();
        System.out.println(demo.romanToInt("III"));
        System.out.println(demo.romanToInt("IV"));
        System.out.println(demo.romanToInt("IX"));
    }

    public int romanToInt(String s) {
        int num = 0;
        int temp_r = -1;
        for (int i = s.length()-1; i >=0; i--) {
            int l = getNum(s.charAt(i));
            if(l<temp_r){
                num -= l;
            }else {
                num += l;
            }
            temp_r = l;
        }
        return num;
    }

    int getNum(char c){
        switch (c){
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                throw new RuntimeException();
        }
    }
}
